﻿package com.rbac.system.controller;

import com.rbac.system.common.RequirePermission;
import com.rbac.system.common.Result;
import com.rbac.system.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role-permission")
@CrossOrigin
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 为角色分配权限
     */
    @PostMapping("/assign")
    @RequirePermission("admin:role:permission:assign")
    public Result<String> assignPermissionsToRole(@RequestParam Long roleId, @RequestBody List<Long> permissionIds) {
        return rolePermissionService.assignPermissionsToRole(roleId, permissionIds);
    }

    /**
     * 获取角色的权限ID列表
     */
    @GetMapping("/role/{roleId}")
    @RequirePermission("admin:role:permission:view")
    public Result<List<Long>> getRolePermissions(@PathVariable Long roleId) {
        return rolePermissionService.getRolePermissions(roleId);
    }

    /**
     * 删除角色的所有权限
     */
    @DeleteMapping("/role/{roleId}")
    @RequirePermission("admin:role:permission:delete")
    public Result<String> removeRolePermissions(@PathVariable Long roleId) {
        return rolePermissionService.removeRolePermissions(roleId);
    }

    /**
     * 删除权限的所有角色关联
     */
    @DeleteMapping("/permission/{permissionId}")
    @RequirePermission("admin:permission:role:delete")
    public Result<String> removePermissionRoles(@PathVariable Long permissionId) {
        return rolePermissionService.removePermissionRoles(permissionId);
    }

    /**
     * 检查并修复角色权限菜单一致性
     */
    @PostMapping("/check-consistency/{roleId}")
    @RequirePermission("admin:role:permission:manage")
    public Result<String> checkAndFixConsistency(@PathVariable Long roleId) {
        return rolePermissionService.checkAndFixConsistency(roleId);
    }
} 

